<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="录音" prop="soundRecording">
      <el-input v-model="dataForm.soundRecording" placeholder="录音"></el-input>
    </el-form-item>
    <el-form-item label="文字描述" prop="content">
      <el-input v-model="dataForm.content" placeholder="文字描述"></el-input>
    </el-form-item>
    <el-form-item label="问题" prop="problem">
      <el-input v-model="dataForm.problem" placeholder="问题"></el-input>
    </el-form-item>
    <el-form-item label="解决方案" prop="solve">
      <el-input v-model="dataForm.solve" placeholder="解决方案"></el-input>
    </el-form-item>
    <el-form-item label="注意内容" prop="attentionContent">
      <el-input v-model="dataForm.attentionContent" placeholder="注意内容"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="uid">
      <el-input v-model="dataForm.uid" placeholder="用户id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          soundRecording: '',
          content: '',
          problem: '',
          solve: '',
          attentionContent: '',
          uid: ''
        },
        dataRule: {
          soundRecording: [
            { required: true, message: '录音不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '文字描述不能为空', trigger: 'blur' }
          ],
          problem: [
            { required: true, message: '问题不能为空', trigger: 'blur' }
          ],
          solve: [
            { required: true, message: '解决方案不能为空', trigger: 'blur' }
          ],
          attentionContent: [
            { required: true, message: '注意内容不能为空', trigger: 'blur' }
          ],
          uid: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/interview/interviewreply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.soundRecording = data.interviewReply.soundRecording
                this.dataForm.content = data.interviewReply.content
                this.dataForm.problem = data.interviewReply.problem
                this.dataForm.solve = data.interviewReply.solve
                this.dataForm.attentionContent = data.interviewReply.attentionContent
                this.dataForm.uid = data.interviewReply.uid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/interview/interviewreply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'soundRecording': this.dataForm.soundRecording,
                'content': this.dataForm.content,
                'problem': this.dataForm.problem,
                'solve': this.dataForm.solve,
                'attentionContent': this.dataForm.attentionContent,
                'uid': this.dataForm.uid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
